<%@page import="dao.Course"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>
<%
    Course course = (Course) session.getAttribute("course");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>添加知识点关联</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<style type="text/css">
body {
	font: normal 16px auto "Trebuchet MS", Verdana, Arial, Helvetica,
		sans-serif;
	color: #4f6b72;
	text-align: left;
	background: #E6EAE9;
}

#container {
	margin-left: auto;
	margin-right: auto;
	width: 800px;
	height: 600px;
	//background: #f8f8f8;
	display: block;
	vertical-align: middle;
}

.ele {
	margin-top: 50px;
	margin-botton: 30px;
	text-align: center;
	width: 100%;
	display: block;
	vertical-align: middle;
}
</style>
<script src="./js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function checkData(){
	var source = document.getElementById("source").value.trim();
	var des = document.getElementById("des").value.trim();
	if(source !="-1" && des !="-1"){
			return true;					
	}else{
		alert("请填写完整！");
		return false;
	}
}
function addChapters(){
	var obj = document.getElementById("chapter1");
	var obj2 = document.getElementById("chapter2");
	var counts = <%=course.getChaptercounts()%>;
	var courseid = "<%=course.getCourseid()%>";
	for(var i=1;i<=counts;i++){
		var value;
		if(i<10){
			value = new Array;
			value.push(courseid);
			value.push("0");
			value.push(i);
		}else{
			value = new Array;
			value.push(courseid);
			value.push(i);
		}
		if ((obj !== null) && obj.hasOwnProperty('options')) {
			obj.options[i] = new Option("第"+i+"章",value.join(""));
		}
		if ((obj2 !== null) && obj2.hasOwnProperty('options')) {
			obj2.options[i] = new Option("第"+i+"章",value.join(""));
		}
	}
}
function delSourceOptions(){	
	var obj = document.getElementById("chapter1");
	for(var i=1;i<obj.options().length;i++){
		
	}
}
function getSourceKnowledges(chapterid){
	
	$.ajax({
        type:"POST",
        url:"./servlet/KnowledgeManageServlet?action=getSourceKnowledges",
        data:{chapterid:chapterid},
        beforeSend:function(){},             
        success:function(data){
        	var obj = document.getElementById("source");
        	var knowledges = data;
        	var strs = knowledges.split(';');
    		for(var i=0;i<strs.length-1;i++){
    			var str1 = strs[i].split(',');
    			var value = str1[0];
    			var string = str1[1];						
    			obj.options[i+1] = new Option(string,value);
    		}
        }            
     });
}
function getDesKnowledges(){
	if($("#chapter2").val() !="choose" && $("#source").val() !="-1"){
	$.ajax({
        type:"POST",
        url:"./servlet/KnowledgeManageServlet?action=getDesKnowledges",
        data:{chapterid:$("#chapter2").val(), source:$("#source").val()},
        beforeSend:function(){},             
        success:function(data){
        	var obj = document.getElementById("des");
        	obj.length = 1;
    		var knowledges = data;
    		var strs = knowledges.split(';');
    		for(var i=0;i<strs.length-1;i++){
    			var str1 = strs[i].split(',');
    			var value = str1[0];
    			var string = str1[1];						
    			obj.options[i+1] = new Option(string,value);
    		}
        }            
     });
	}
}
window.onload = addChapters();
</script>
</head>

<body>
	<div id="container">
		<form name="addPrelevanceForm" method="post"
			action="./servlet/PrelevanceManagerServlet?action=add"
			onsubmit="return checkData()">
			<div class="ele">
				<label>源知识点：</label><br> <label>选择章节：</label> <select
					name="chapter1" id="chapter1"
					onchange="getSourceKnowledges(this.value)">
					<option value="choose" selected="selected">请选择章节</option>
				</select> &nbsp;&nbsp;&nbsp;&nbsp; 
				<select name="source" id="source" onchange="getDesKnowledges()">
					<option value="-1" selected="selected">请选择源知识点</option>
				</select>
			</div>

			<div class="ele">
				<label>相关知识点：</label><br> <label>选择章节：</label> 
				<select name="chapter2" id="chapter2" onchange="getDesKnowledges()">
					<option value="choose" selected="selected">请选择章节</option>
				</select> &nbsp;&nbsp;&nbsp;&nbsp; <select name="des" id="des">
					<option value="-1" selected="selected">请选择相关知识点</option>
				</select>
			</div>
			<div class="ele">
				<input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp; <input
					type="reset" value="取消">
			</div>
		</form>
	</div>
	<script type="text/javascript">
	var obj = document.getElementById("chapter1");
	var obj2 = document.getElementById("chapter2");
	var counts = <%=course.getChaptercounts()%>;
	var courseid = "<%=course.getCourseid()%>";
	for(var i=1;i<=counts;i++){
		var value;
		if(i<10){
			value = courseid + "0" +i;
		}else{
			value = courseid + i;
		}
		obj.options[i] = new Option("第"+i+"章",value);
		obj2.options[i] = new Option("第"+i+"章",value);
	}
	</script>
	<%
	    if (request.getAttribute("success") != null) {
				String str = (String) request.getAttribute("success");
				if ("true".equals(str)) {
				    out.write("<script>alert('添加成功！')</script>");
				} else if ("false".equals(str)) {
				    out.write("<script>alert('添加失败！')</script>");
				}
	    }
	%>
</body>
</html>
